﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TnaguTetris
{
    /// <summary>
    /// Siia tuleb kõik mis on seotud klotside kasutamisega.
    /// </summary>
    class Klotsid
    {
        // START: ERINEVAD KLOTSID
        public int[, ,] blocks = new int[7, 4, 2] {
	        { { 5, 1 }, { 4, 0 }, { 5, 0 }, { 6, 0 } }, // T
	        { { 5, 1 }, { 4, 1 }, { 5, 0 }, { 6, 0 } }, // S
	        { { 5, 1 }, { 4, 0 }, { 5, 0 }, { 6, 1 } }, // Z
	        { { 5, 1 }, { 5, 0 }, { 5, 2 }, { 5, 3 } }, // I
	        { { 4, 1 }, { 4, 0 }, { 4, 2 }, { 5, 2 } }, // L
	        { { 5, 1 }, { 5, 0 }, { 5, 2 }, { 4, 2 } }, // J
	        { { 4, 0 }, { 5, 0 }, { 4, 1 }, { 5, 1 } }  // O
        };
        public string[] blockNames = new string[7] { "T", "S", "Z", "I", "L", "J", "O" };
        // END: ERINEVAD KLOTSID

        public int[,] current_block = new int[4, 2]; // hetke klots mis alla kukub (v6etakse block massiivist)
        public int[,] next_block = new int[4, 2]; // j2rgmine klots
        public string nextBlockName;
        public int currentBlockColor;
        public int nextBlockColor;
        Random myRandom = new Random();
        public int blockDownTime = 0;
        public bool didBlockMove = false;
        public int x = 0;
        public int y = 0;

        /// <summary>
        /// Genereerib suvalise numbri antud vahemikus
        /// </summary>
        /// <param name="min">Min väärtus</param>
        /// <param name="max">Max väärtus</param>
        /// <returns>Tagastab suvalise numbri antud vahemikus</returns>
        public int RandomNumber(int min, int max)
        {
            return myRandom.Next(min, max);
        }

        /// <summary>
        /// Genereerib uue klotsi blocks massiivist
        /// </summary>
        public void generateFromBlocks()
        {
            if (next_block[0, 0] == 0)
            {
                int rand = RandomNumber(0, blocks.GetLength(0));
                nextBlockColor = rand;
                nextBlockName = blockNames[rand];
                for (int i = 0; i < blocks.GetLength(1); i++)
                {
                    next_block[i, 0] = blocks[rand, i, 0];
                    next_block[i, 1] = blocks[rand, i, 1];
                }

                rand = RandomNumber(0, blocks.GetLength(0));
                currentBlockColor = rand;
                nextBlockName = blockNames[rand];
                for (int j = 0; j < blocks.GetLength(1); j++)
                {
                    current_block[j, 0] = blocks[rand, j, 0];
                    current_block[j, 1] = blocks[rand, j, 1];
                }
            }
            else
            {
                currentBlockColor = nextBlockColor;
                for (int i = 0; i < next_block.GetLength(0); i++)
                {
                    current_block[i, 0] = next_block[i, 0];
                    current_block[i, 1] = next_block[i, 1];
                }

                int rand = RandomNumber(0, blocks.GetLength(0));
                nextBlockColor = rand;
                nextBlockName = blockNames[rand];
                for (int j = 0; j < blocks.GetLength(1); j++)
                {
                    next_block[j, 0] = blocks[rand, j, 0];
                    next_block[j, 1] = blocks[rand, j, 1];
                }
            }
            switch (nextBlockName)
            {
                case "T":
                case "S":
                case "Z":
                    x = 0;
                    y = 24;
                    break;
                case "L":
                    x = 12;
                    y = 12;
                    break;
                case "J":
                    x = 12;
                    y = 12;
                    break;
                case "O":
                    x = 12;
                    y = 24;
                    break;
                case "I":
                    x = 0;
                    y = 0;
                    break;
            }
        }

        /// <summary>
        /// Kas hall ruut saab alla poole liikuda
        /// </summary>
        /// <param name="i">x koordinaat</param>
        /// <param name="j">y kordinaat</param>
        /// <param name="board">paigutus laual</param>
        /// <returns>Kas saab või mitte</returns>
        public bool CanGrayMove(int i, int j, int[,] board)
        {
            if (board[i, j] == 1)
            {
                if (board.GetLength(0) - 1 > i)
                {
                    if (board[i + 1, j] == 1)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
                if (i == 19)
                {
                    return false;
                }
                return true;
            }
            return false;
        }


        /// <summary>
        /// kontrollib kas klots saab liikuda left-vasakule, right-paremale v6i down-alla
        /// </summary>
        /// <param name="direction">Kuhu suunas</param>
        /// <param name="current_block">Hetkel kästiletav element</param>
        /// <param name="board">Paigutus laual</param>
        /// <returns>Jah saab = true</returns>
        public bool CanMove(string direction, int[,] current_block, int[,] board)
        {
            for (int i = 0; i < current_block.GetLength(0); i++)
            {
                // kas vasakule saab liikuda?
                if (direction == "left")
                {
                    // kas pole vasakule seinale liiga l2hedal
                    if (current_block[i, 0] == 0)
                    {
                        return false;
                    }
                    // kas vasakul pole juba klotsi ees
                    if (board[current_block[i, 1], current_block[i, 0] - 1] == 1)
                    {
                        return false;
                    }
                }
                // kas paremale saab liikuda?
                if (direction == "right")
                {
                    // kas pole paremale seinale liiga l2hedal
                    if (current_block[i, 0] == 9)
                    {
                        return false;
                    }
                    // kas paremal pole juba klotsi ees
                    if (board[current_block[i, 1], current_block[i, 0] + 1] == 1)
                    {
                        return false;
                    }
                }
                // kas alla saab liikuda?
                if (direction == "down")
                {
                    // kontrollib kas on all 22res
                    if (current_block[i, 1] == 19)
                    {
                        return false;
                    }
                    // kontrollib kas all on juba klots ees
                    if (board[current_block[i, 1] + 1, current_block[i, 0]] == 1)
                    {
                        return false;
                    }
                }
            }
            // k6ik ok? siis saab ju liikuda
            return true;
        }
    }
}
